package com.FH.organization.sys_operator;

import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/8/3.
 */
@Stateless
public class SysOperatorDAOImpl extends AccessDB implements SysOperatorDAO{


    /**
     * 功能操作表查询实现类
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getSysOperatorInfo(String search, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(2) from sys_operator where 1=1 ";

            String sql_w ="";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(page);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok + 1) + "," + size + "";
            }

            if(json.has("contract_id")){
                sql_w += " AND contract_id = '"+json.getString("contract_id")+"'";
            }
            if(json.has("member_id")){
                sql_w += " AND member_id = '"+json.getString("member_id")+"'";
            }
            if(json.has("sys_role_id")){
                sql_w += " AND sys_role_id = '"+json.getString("sys_role_id")+"'";

            }
            String sql_ok = sqlexit+sql_w ;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String []> seresult = new ArrayList<String []>();

            List<SysOperatorShowDTO> result = new ArrayList<SysOperatorShowDTO>();

            if(!existName.equals("0")){

                String sql ="select c.sys_operator_id,c.sys_role_id,c.sys_operator_name,c.sys_operator_code," +
                        "c.sys_operator_url,c.org_role_modify_time,c.sys_operator_note,a.sys_role_name from" +
                        " sys_operator c,sys_role a where c.sys_role_id=a.sys_role_id" +" AND c.sys_role_id = '"+json.getString("sys_role_id")+"'" + sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++){

                    SysOperatorShowDTO sysOperatorShowDTO = new SysOperatorShowDTO();

                    sysOperatorShowDTO.setSys_operator_id(seresult.get(i)[1]);
                    sysOperatorShowDTO.setSys_role_id(seresult.get(i)[2]);
                    sysOperatorShowDTO.setSys_operator_name(seresult.get(i)[3]);
                    sysOperatorShowDTO.setSys_operator_code(seresult.get(i)[4]);
                    sysOperatorShowDTO.setSys_operator_url(seresult.get(i)[5]);
                    sysOperatorShowDTO.setOrg_role_modify_time(seresult.get(i)[6]);
                    sysOperatorShowDTO.setSys_operator_note(seresult.get(i)[7]);
                    sysOperatorShowDTO.setSys_role_name(seresult.get(i) [8]);

                    result.add(sysOperatorShowDTO);

                }
            }
             if(seresult.size()!=0) {
                resultDTO.setValues(result);
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            }else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return resultDTO;
    }


    /**
     * 增加功能操作
     * @param dataDTO
     * @return
     */
    @Override
    public ResultDTO addSysOperatorInfo(SysOperatorPullDTO dataDTO) {

        ResultDTO resultDTO = new ResultDTO();//构建返回对象

        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String time=df.format(new Date());

            String sql = "";

            sql += "insert into sys_operator(sys_operator_id,sys_role_id,sys_operator_name," +
                    "sys_operator_code,sys_operator_url,org_role_modify_time,sys_operator_note) values" +
                    " ('" + uuid + "','" + dataDTO.getSys_role_id() + "','" + dataDTO.getSys_operator_name() + "','"
                    + dataDTO.getSys_operator_code() + "','" + dataDTO.getSys_operator_url() + "','" + time + "','" + dataDTO.getSys_operator_note() + "')";

            int result = this.addReportingTo(sql);

        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }


}
